﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Enter.Entity;
using Enter.BizLogic;
using System.Data;
using System.Web.UI.HtmlControls;
using Enter.Common;

namespace Enter.Web.SystemAdmin.sys
{
    public partial class RoleRightsForm : System.Web.UI.Page
    {
        private Sys_RoleEntity _role;

        protected void Page_Load(object sender, EventArgs e)
        {


            _role = new Sys_RoleBLLSub().GetObjectByID(int.Parse(Request["ID"]));

            if (Page.IsPostBack) return;


            BindMenu();
        }

        /// <summary>
        /// 绑定人员菜单权限
        /// </summary>
        private void BindMenu()
        {
            try
            {
               
                string selectRights = _role.Rights;

                DataTable menus = new Sys_PopMenuListBLLSub().GetValidMenus();

                HtmlTableRow tableRow = new HtmlTableRow();
                HtmlTableCell tableCell = new HtmlTableCell();
                int i = 0;
                int tcount = 0;

                DataRow[] parentMenus = menus.Select("parentNo='0'", "menuOrder");

                for (i = 0; i < parentMenus.Length; i++)
                {
                    string menuNo = parentMenus[i]["menuNo"].ToString();
                    string menuText = parentMenus[i]["menuText"].ToString();

                    string ckStr = selectRights.IndexOf(menuNo) >= 0 ? "checked" : "";

                    if (i % 3 == 0)
                    {
                        tableRow = new HtmlTableRow();
                    }

                    string prStr = "<input type='checkbox' name='menuNo' value='" + menuNo + "' " + ckStr + " menuPath='_0_' onclick='checkMenu(this)'>";
                    prStr += (i + 1).ToString() + "." + menuText;
                    prStr += "<br>" + BindChildMenuInfo(menus, selectRights, menuNo, "_0_", 1);

                    tableCell = new HtmlTableCell();
                    tableCell.InnerHtml = prStr;
                    tableCell.Attributes.Add("class", "MTGridText");
                    tableCell.Attributes.Add("valign", "top");
                    tableRow.Cells.Add(tableCell);

                    if (i % 3 == 2)
                    {
                        valTable.Rows.Add(tableRow);
                    }
                    tcount++;
                }
                if (i % 3 != 0)
                {
                    tableCell = new HtmlTableCell();
                    tableCell.InnerHtml = "&nbsp;";
                    tableCell.Attributes.Add("class", "MTGridText");
                    tableCell.Attributes.Add("colspan", (3 - i % 3).ToString());
                    tableRow.Cells.Add(tableCell);

                    valTable.Rows.Add(tableRow);
                }
            }
            catch (Exception ee)
            {
                Response.Write(ee.ToString());
            }
        }

        /// <summary>
        /// 绑定子菜单权限

        /// </summary>
        /// <param name="userPriList"></param>
        /// <param name="parentNo"></param>
        /// <param name="menuPath"></param>
        /// <param name="layer"></param>
        /// <returns></returns>
        private string BindChildMenuInfo(DataTable allDT, string userPriList, string parentNo, string menuPath, int layer)
        {
            int i, j;

            string prStr = "";
            string ckStr = "", menuNo = "", menuText = "";
            string nowMenuPath = "", retStr = "";
            DataRow[] dr;

            dr = allDT.Select("parentNo='" + parentNo + "' and menuText<>'-'", "menuOrder");
            for (i = 0; i < dr.Length; i++)
            {
                menuNo = dr[i]["menuNo"].ToString();
                menuText = dr[i]["menuText"].ToString();

                if (userPriList.IndexOf(menuNo) >= 0)
                {
                    ckStr = "checked";
                }
                else
                {
                    ckStr = "";
                }

                nowMenuPath = menuPath + parentNo + "_";

                prStr = "<input type='checkbox' name='menuNo' value='" + menuNo + "' " + ckStr + " menuPath='" + nowMenuPath + "' onclick='checkMenu(this)'>";
                prStr += (i + 1).ToString() + "." + menuText;

                for (j = 0; j < layer; j++)
                {
                    prStr = "&nbsp;&nbsp;" + prStr;
                }

                retStr += prStr + "<br>" + BindChildMenuInfo(allDT, userPriList, menuNo, nowMenuPath, layer + 1);
            }
            return retStr;
        }

        protected void btnApply_Click(object sender, EventArgs e)
        {
            string rights = Request.Form["menuNo"] == null ? "" : Request.Form["menuNo"].Replace(" ", "");
            _role.Rights = rights;
             Sys_RoleBLLSub bll=new Sys_RoleBLLSub();
            bool result = bll.UpdateRoleRights(_role);
            if(result)
            {
                Utils.throwErrorGotoURL("操作成功", "RoleList.aspx");
            }
        }
    }
}